﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Com.Seezt.Skins;
using Components;
using UI.Lib;
using UI.Lib.Factory;
using System.Collections;

namespace UI.StorageManagement
{
    public partial class FrmSendAdjust : UI.ParentForm.BaseMedit
    {
        string key = null;
        public FrmSendAdjust(string modulecode, string action, string pk = null)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.key = pk;

            //回写的方法
            this.SaveCallFuntion = "Sto8a00Manager$MeditSaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = false;
            this.MaxStateFlag = true;//打开页面后让窗口最大化

            if (string.IsNullOrEmpty(this.key))
            {
                SetToolButton();
            }

            this.Text = "发货调整";

            LoadData();
        }

        //加载修改数据
        private void LoadData()
        {
            if (!string.IsNullOrEmpty(this.key))
            {
                base.LoadEditData(null, string.Format(" f.listid ='{0}' ", this.key));
                base.LoadDtlData(null, string.Format(" f.dtldocid ='{0}' ", this.key));
            }
        }

        protected override void PageInit()
        {
            this.tbSummary.DefaultWidth = 100;
            tspSave.Visible = true;
        }

        private void SetToolButton()
        {
            tspDtlTop.Items.Insert(0, CreateToolBtn("T", "提取源单据", btnGetData_Click));
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            if (AutoCheck() == false)
                return;

            var orderListid = this.tbSummary.GetValue("refid");

            var where = QB.Entity.AddParam("@orderid", orderListid);

            //加载顶层节点
            var data = CommonHelper.Entity.ExecFun("getorderdetails", where);

            BindEditData(data);
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("productcode" == name || "spno" == name)
            {
                where = string.Format("status ='1' {0} {1}", Context.GetAppKeyWhere("muser", true),
                    "spno" == name ? " AND spno IS NOT NULL " : " ");

                string table = string.Format(@"(
SELECT a.productcode, a.status, a.muser, a.productname, a.bordqty, a.bprice as bnprice,
ISNULL((CASE WHEN '{0}' = '' THEN (SELECT SUM(storageqty) FROM Sto6a00 z WHERE z.pid = a.listid) ELSE e.storageqty END),0) AS storageqty,
a.listid, c.spno, a.pamt, a.umdesc, a.pvlistid
FROM
Pot3a00_view a
LEFT JOIN Bas1a13 c ON a.listid = c.pid AND c.slistid = '{1}'
LEFT JOIN Sto6a00 e ON a.listid = e.pid AND e.storageid = '{0}' ) T",
                Validation.ToString(this.tbSummary.GetValue("storageid")),
                Validation.ToString(this.tbSummary.GetValue("sellerid"))
                );

                others = new string[] { table };
            }

            return true;
        }

        public override void AfterDtlPopWin(string name, Hashtable data, bool ischange)
        {
            //if (ischange)
            {
                if ("productcode" == name)
                {
                    this.dgvDtl.SetValue("dtlqty", "0");
                    this.dgvDtl.SetValue("ppreqty", "0");
                    this.dgvDtl.SetValue("dtlprice", "0");
                    this.dgvDtl.SetValue("dtltotalamt", "0");
                    this.dgvDtl.SetValue("actqty1", "0");
                    this.dgvDtl.SetValue("actqty2", "0");
                }
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("refdocno" == name)
            {
                where = string.Format(" a.status IN ('2', '4', '5', '6', '7') {0}", Context.GetAppKeyWhere("a.muser", true));
            }

            return true;
        }

        protected override void AfterDtlValueChanged(string name, object value)
        {
            if ("actqty1" == name || "actprice1" == name) //计算收货金额
            {
                var qty = Validation.ToFloat(this.dgvDtl.GetValue("actqty1"));
                var ordprice = Validation.ToFloat(this.dgvDtl.GetValue("actprice1"));

                this.dgvDtl.SetValue("actamt1", Validation.ToDouble(qty * ordprice, 3));

                CalcDtlQtyAndAmt();
            }
            else if ("actqty2" == name)
            {
                CalcDtlQtyAndAmt();
            }
        }

        private void CalcDtlQtyAndAmt()
        {
            decimal sumQty = 0;
            decimal sumAmt = 0;
            foreach (DataGridViewRow row in this.dgvDtl.Rows)
            {
                var qty1 = Validation.ToFloat(row.Cells["dtlqty"].Value) + Validation.ToFloat(row.Cells["ppreqty"].Value);
                var qty2 = Validation.ToFloat(row.Cells["actqty1"].Value) + Validation.ToFloat(row.Cells["actqty2"].Value);
                var amt1 = Validation.ToFloat(row.Cells["dtltotalamt"].Value);
                var amt2 = Validation.ToFloat(row.Cells["actamt1"].Value);

                sumQty += (qty2 - qty1);
                sumAmt += (amt2 - amt1);
            }

            this.tbSummary.SetValue("totalamt", sumAmt);
            this.tbSummary.SetValue("totalqty", sumQty);
        }
    }
}
